package com.ase.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gyhstart
 * @create 2021/3/13 - 14:47
 **/
public class First {
    public static void main(String[] args) {
        int[][] arr = {{1, 5}};
        int[][] merge = merge(arr);
        System.out.print("[");
        for (int i = 0; i < merge.length; i++) {
            System.out.print("[" + merge[i][0]);
            System.out.print("," + merge[i][1] + "]");

        }
        System.out.print("]");
    }

    public static int[][] merge(int[][] intervals) {
        List<int[]> newIntervals = new ArrayList<>();
        int i = 0;
        while (i < intervals.length) {
            if (i + 1 == intervals.length) {
                newIntervals.add(new int[]{intervals[i][0], intervals[i][1]});
                break;
            }
            if (intervals[i][1] >= intervals[i + 1][0]) {
                newIntervals.add(new int[]{intervals[i][0], intervals[i + 1][1]});
                i++;
            } else {
                newIntervals.add(new int[]{intervals[i][0], intervals[i][1]});
            }
            i++;
        }
        return newIntervals.toArray(new int[0][]);
    }
}
